package com.ts.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.ts.entity.Book;
import com.ts.util.DB;
@Results({
	@Result(name="book_add_success",location="book_list",type="redirectAction"),
	@Result(name="list_success",location="/home.jsp"),
	@Result(name="modify_success",location="/edit.jsp",type="redirect"),
	@Result(name="book_modify_success",location="book_list",type="redirectAction"),
	@Result(name="remove_success",location="book_list",type="redirectAction")
})
public class BookAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Book book;
	private Connection connection;
	private PreparedStatement preparedStatement;
	private ResultSet resultSet;
	private static final String ADD = "insert into book values(null, ?, ?, ?, ?)";
	private static final String LIST = "select * from book";
	private static final String MODIFY = "select * from book where id=?";
	private static final String EDIT = "update book set title=?, author=?, publisher=?, time=? where id=?";
	private static final String REMOVE = "delete from book where id=?";

	/** 添加图书 */
	@Action("book_add")
	public String add() throws Exception {
		connection = DB.getConnection();
		preparedStatement = connection.prepareStatement(ADD);
		preparedStatement.setString(1, book.getTitle());
		preparedStatement.setString(2, book.getAuthor());
		preparedStatement.setString(3, book.getPublisher());
		preparedStatement.setString(4, book.getTime());
		preparedStatement.executeUpdate();
		DB.close(resultSet, preparedStatement, connection);
		return "book_add_success";
	}

	/**
	 * 查询所有图书
	 * 
	 * @throws Exception
	 */
	@Action("book_list")
	public String list() throws Exception {
		connection = DB.getConnection();
		preparedStatement = connection.prepareStatement(LIST);
		resultSet = preparedStatement.executeQuery();
		List<Book> books = new ArrayList<Book>();
		while (resultSet.next()) {
			Book book = new Book(resultSet.getInt("id"),
					resultSet.getString("title"),
					resultSet.getString("author"),
					resultSet.getString("publisher"),
					resultSet.getString("time"));
			books.add(book);
		}
		DB.close(resultSet, preparedStatement, connection);
		getSession().put("books", books);
		return "list_success";
	}

	/** 修改功能的取内容方法 */
	@Action("book_modify")
	public String modify() throws Exception {
		connection = DB.getConnection();
		preparedStatement = connection.prepareStatement(MODIFY);
		preparedStatement.setInt(1, book.getId());
		resultSet = preparedStatement.executeQuery();
		resultSet.next();
		Book book = new Book(resultSet.getInt("id"),
				resultSet.getString("title"), resultSet.getString("author"),
				resultSet.getString("publisher"), resultSet.getString("time"));
		getSession().put("book", book);
		return "modify_success";
	}

	/** 修改功能的取内容后修改方法 */
	@Action("book_edit")
	public String edit() throws Exception {
		connection = DB.getConnection();
		preparedStatement = connection.prepareStatement(EDIT);
		preparedStatement.setString(1, book.getTitle());
		preparedStatement.setString(2, book.getAuthor());
		preparedStatement.setString(3, book.getPublisher());
		preparedStatement.setString(4, book.getTime());
		preparedStatement.setInt(5, book.getId());
		preparedStatement.executeUpdate();
		DB.close(resultSet, preparedStatement, connection);
		return "book_modify_success";
	}
	/** 修改功能的取内容后修改方法 */
	@Action("book_remove")
	public String remove() throws Exception {
		connection = DB.getConnection();
		preparedStatement = connection.prepareStatement(REMOVE);
		preparedStatement.setInt(1, book.getId());
		preparedStatement.executeUpdate();
		return "remove_success";
		
	}

	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}

}
